﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace JustinAngel.MindwaveSensor.WP8.Controls
{
    /// <summary>
    /// Displays a buttons to connect or disconnect from a mindwave sensor based on the sensor's state.
    /// </summary>
    [TemplatePart(Type = typeof(Button), Name = "ConnectButton")]
    [TemplatePart(Type = typeof(Button), Name = "DisconnectButton")]
    public class MindwaveConnectButton : MindwaveControl
    {
        public MindwaveConnectButton()
        {
            this.DefaultStyleKey = typeof(MindwaveConnectButton);
        }

        protected override void UpdateVisualState()
        {
            if (MindwaveSensor.IsConnected)
                VisualStateManager.GoToState(this, "Connected", false);
            else
                VisualStateManager.GoToState(this, "Disconnected", false);
        }


        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var connectButton = (Button)GetTemplateChild("ConnectButton");
            if (connectButton != null)
            {
                connectButton.Click += ConnectButtonClick;
            }

            var disconnectButton = (Button)GetTemplateChild("DisconnectButton");
            if (disconnectButton != null)
            {
                disconnectButton.Click += DisonnectButtonClick;
            }
        }

        void DisonnectButtonClick(object sender, RoutedEventArgs e)
        {
            MindwaveSensor.Stop();
        }

        void ConnectButtonClick(object sender, RoutedEventArgs e)
        {
            MindwaveSensor.Start();
        }
    }
}
